﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.Domain;
using MShop.Core.DataAccess;
using MShop.Core;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class MenuPageController : SecureController
    {
        private readonly IMenuDao _MenuDao;
        private readonly IShopContext _ShopContext;
        private readonly ICommonDao<MenuPage> _MenuPageDao;
        public MenuPageController(IMenuDao menuDao, IShopContext shopContext, ICommonDao<MenuPage> menuPageDao)
        {
            _ShopContext = shopContext;
            _MenuDao = menuDao;
            _MenuPageDao = menuPageDao;
        }

        public ActionResult Index(int id)
        {
            var q = from item in _MenuPageDao.GetAll() where item.MenuId == id select item;
            return View(q);
        }

        public ActionResult Delete(int id)
        {
            var q = (from item in _MenuPageDao.GetAll() where item.Id == id select item).First();
            _MenuPageDao.Delete(q);
            _MenuPageDao.SaveChanges();
            return RedirectToAction("INdex", new { id = q.MenuId });
        }


        [HttpPost]
        public ActionResult Index(MenuPage page,int id)
        {
            page.MenuId = id;
            if (page.Action == null || page.Controller == null)
            {
                return RedirectToAction("index");
            }
            if (string.IsNullOrEmpty(page.Area))
            {
                page.Url = string.Concat("/", page.Controller.ToLower(), "/", page.Action.ToLower());
            }
            else
            {
                page.Url = string.Concat("/", page.Area.ToLower(), "/", page.Controller.ToLower(), "/", page.Action.ToLower());
            }
            _MenuPageDao.Save(page);
            _MenuPageDao.SaveChanges();
            return RedirectToAction("index");
           
        }
    }
}
